mod start;
use start::setup;

use tauri::{async_runtime::spawn, Manager};

#[tauri::command]
fn handle_window_event(event: String) {
    println!("Received window event: {}", event);
}
// 新增窗口控制命令
#[tauri::command]
async fn resize_window(app: tauri::AppHandle, width: f64, height: f64) -> Result<(), String> {
    let window = app.get_window("main").ok_or("找不到主窗口")?;

    window
        .set_size(tauri::Size::Logical(tauri::LogicalSize { width, height }))
        .map_err(|e| e.to_string())?;

    window.center().map_err(|e| e.to_string())?;
    Ok(())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_log::Builder::new().build())
        .plugin(tauri_plugin_updater::Builder::new().build())
        .plugin(tauri_plugin_process::init())
        .plugin(tauri_plugin_websocket::init())
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_window_state::Builder::new().build())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_upload::init())
        .plugin(tauri_plugin_sql::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_opener::init())
        // 启动动画
        .setup(|app| {
            // // 显式创建主窗口
            // let _main_window = WindowBuilder::new(app, "main")
            //     .url(WindowUrl::App("/index.html".into()))
            //     .inner_size(350.0, 475.0)
            //     .center()
            //     .resizable(false)
            //     .build()
            //     .expect("主窗口创建失败");
            //  启动初始化任务
            spawn(setup(app.handle().clone()));
            Ok(())
        })
        // 注册 Rust 后端函数，暴露给前端调用
        .invoke_handler(tauri::generate_handler![
            handle_window_event,
            resize_window // 新增命令
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
